import 'package:any_reader/config/resource_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'public.dart';

main() async {
  Provider.debugCheckInvalidValueType = null;
  WidgetsFlutterBinding.ensureInitialized();
  runApp(App(future: LoadConfig.preLoad()));
}

/// 用于项目初始化之前显示的页面
class SplashImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      color: Color(0xFF111427), //背景色
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            alignment: Alignment.bottomCenter,
            child: Image.asset(
              ImageHelper.wrapAssets("launch_top.png"),
              fit: BoxFit.fitHeight,
              height: 150,
            ),
          ),
        ],
      ),
    );
  }
}

class App extends StatelessWidget {
  /// 在App运行之前,需要初始化的异步操作
  /// 如果存在多个,可以使用[Future.wait(futures)]来合并future后传入
  final Future future;

  const App({this.future});
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return FutureBuilder(
        future: future,
        builder: (context, snapshot) {
          /// 在异步操作时,显示的页面
          print(snapshot.connectionState);
          print("初始化state");
          if (snapshot.connectionState != ConnectionState.done) {
            return SplashImage();
          }
          return MultiProvider(
              providers: providers,
              child:
                  Consumer<ThemeModel>(builder: (context, themeModel, child) {
                return MaterialApp(
                  builder: (context, child) {
                    return ScrollConfiguration(
                      behavior: MyBehavior(),
                      child: child,
                    );
                  },
                  theme: ThemeData(
                    primaryColor: Color(0xFF111427),
                    scaffoldBackgroundColor: Color(0xFF111427),
                  ),
                  title: "AnyReader",
                  debugShowCheckedModeBanner: false,
                  onGenerateRoute: AppConfig.router.generator,
                  home: LayoutComponent(),
                  color: Color(0xFF111427),
                  localizationsDelegates: [
                    //此处
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                  ],
                  supportedLocales: [
                    //此处
                    const Locale('zh', 'CH'),
                    const Locale('en', 'US'),
                  ],
                );
              }));
        });
  }
}

class MyBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(
      BuildContext context, Widget child, AxisDirection axisDirection) {
    //去除Android越界回弹
    return child;
  }
}
